package com.mrobuy.jdvc.dao.impl;

import com.gavel.utils.StringUtils;
import com.mrobuy.jdvc.entity.VCProduct;
import com.mrobuy.util.Pagination;
import com.mrobuy.jdvc.dao.VCBrandDao;
import com.mrobuy.jdvc.dao.VCCategotyDao;
import com.mrobuy.jdvc.entity.VCCategory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.Collections;
import java.util.List;

@Repository("vCategotyDao")
public class VCCategoryDaoImpl  extends CommonDaoImpl implements VCCategotyDao {

    private static Logger LOG = LoggerFactory.getLogger(VCBrandDao.class);

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Override
    public List<VCCategory> list() {

        StringBuilder sql = new StringBuilder();
        sql.append(" SELECT * from VC_CATEGORY");
        try {
            List<VCCategory> list = jdbcTemplate.query(sql.toString(), new BeanPropertyRowMapper(VCCategory.class));
            return list;
        } catch (Exception e) {
            LOG.error(e.getMessage());
        }

        return Collections.EMPTY_LIST;
    }

    @Override
    public Pagination load(int start, int length) {


        Pagination pagination = new Pagination();

        try {
            int count = jdbcTemplate.queryForObject("select count(*) from VC_CATEGORY", Integer.class);
            List<VCCategory> list = jdbcTemplate.query("select * from VC_CATEGORY limit ?, ?", new Object[] { start, length }, new BeanPropertyRowMapper(VCCategory.class));


            pagination.setRecordsTotal(count);
            pagination.setRecordsFiltered(count);
            pagination.setData(list);
        } catch (Exception e) {
            LOG.error(e.getMessage());
        }

        return pagination;
    }

    @Override
    public VCCategory load(String cid1) {
        if (StringUtils.isBlank(cid1)) {
            return null;
        }
        try {
            VCCategory category = (VCCategory)jdbcTemplate.queryForObject("select * from VC_CATEGORY where ID = ?", new Object[]{cid1}, new BeanPropertyRowMapper(VCCategory.class));
            return category;
        } catch (Exception e) {
            LOG.error(e.getMessage());
        }

        return null;
    }

    @Override
    public VCCategory load(String cid, String vendor) {
        if (StringUtils.isBlank(cid)) {
            return null;
        }
        try {
            VCCategory category = (VCCategory)jdbcTemplate.queryForObject("select * from VC_CATEGORY where ID = ? and VCCODE = ? ", new Object[]{cid, vendor}, new BeanPropertyRowMapper(VCCategory.class));
            return category;
        } catch (Exception e) {

        }

        return null;
    }

    @Override
    public List<VCCategory> list(String vccode) {
        StringBuilder sql = new StringBuilder();
        sql.append(" SELECT * from VC_CATEGORY where VCCODE = ? ");
        try {
            List<VCCategory> list = jdbcTemplate.query(sql.toString(), new BeanPropertyRowMapper(VCCategory.class), vccode);
            return list;
        } catch (Exception e) {
            LOG.error(e.getMessage());
        }

        return Collections.EMPTY_LIST;
    }
}
